package xuanzhen.pro.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xuanzhen.pro.pojo.CofeGain;
import xuanzhen.pro.service.CofeService;
import xuanzhen.wx.pojo.AccessToken;
import xuanzhen.wx.pojo.Jsticket;
import xuanzhen.wx.pojo.WxUserMsg;
import xuanzhen.wx.service.CoreService;
import xuanzhen.wx.util.fina.WxSet;
@Controller
@RequestMapping("/cofe")
public class CofeController {
	@Resource
	CoreService coreService;
	@Resource
	CofeService cofeService;
	/**
	 * 跳转授权页面
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String in(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)throws Exception{
		session.setAttribute("opid", "ceshi3");
		String openid = "ceshi3";
		if(cofeService.selectCofeGain(openid)==null){
			CofeGain cg = new CofeGain();
			cg.setOpenid("ceshi3");
			cofeService.saveCofeGain(cg);
		}else {
			if(!"none".equals(cofeService.selectCofeGain("ceshi3").getGainState())){
				request.setAttribute("gain", "finish");
			}
		}
		return "cofe";
	}
	/**
	 * 跳转授权页面
	 */
	@RequestMapping(value = "/toGain.html", method = RequestMethod.GET)
	public void toRight(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.sendRedirect(coreService.getCofeRight());
	}
	/**
	 * 获取授权
	 */
	@RequestMapping(value = "/GainCofe.html", method = RequestMethod.GET)
	public String getRight(Model model, HttpSession session, HttpServletRequest request) throws Exception {
		// 获取授权后code
		String code = request.getParameter("code");
		session.setAttribute("code", code);
		String state = request.getParameter("state");
		// 获取token
		AccessToken token = coreService.getAccessToken(code, state);
		System.out.println(token);
		// 获取用户微信信息
		WxUserMsg umsg = coreService.catchUserMsg(token);
		String opid= umsg.getOpenid();
		if(cofeService.selectCofeGain(opid)==null){
			CofeGain cg = new CofeGain();
			cg.setOpenid(opid);
			cofeService.saveCofeGain(cg);
		}else {
			if(!("none".equals(cofeService.selectCofeGain(opid).getGainState()))){
				request.setAttribute("gain", "finish");
			}
		}
		// 获得js票
		AccessToken tokenCe = coreService.getAccessTokenC();
		Jsticket ticket = coreService.getJsticket(tokenCe);
		session.setAttribute("ticket", ticket);
		System.out.println(ticket);
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		// 获取时间戳
		long timestamp = System.currentTimeMillis() / 1000;
		session.setAttribute("timestamp", timestamp);
		// 生成随机字符串
		String noncestr = RandomStringUtils.randomAlphanumeric(16);
		session.setAttribute("noncestr", noncestr);
		// appid
		session.setAttribute("appid", WxSet.APPID);
		// 当前网页url
		String url = "";
		url = request.getScheme() + "://" + request.getServerName() + request.getRequestURI();
		if (request.getQueryString() != null) {
			url += "?" + request.getQueryString();
		} 		
				
			/*	"http://run.xuanzhen-tech.com/wx_project/user/getRight.html?code=" + code + "&state=STATE";*/
		// 签名
		String signature = coreService.getSignature(url, timestamp + "", noncestr, ticket.getTicket());
		session.setAttribute("signature", signature);
		
		session.setAttribute("opid", opid);

		return "cofe";
	}
	/**
	 * 领取咖啡
	 */
	@RequestMapping(value = "/CofeGain.html", method = RequestMethod.POST)
	public String CofeGain(CofeGain cg, HttpSession session, HttpServletRequest request) throws Exception {
		cg.setGainState("finish");
		CofeGain cofe = cofeService.updateCofeGain(cg);
		request.setAttribute("gain", cofe.getGainState());
		return "cofe";
	}
}
